import { defineStore } from "pinia"
import useCommonStore from "@/store/common"
import { getLocalStorage, removeLocalStorage, setLocalStorage } from "@/utils/tools"

const useSearchStore = defineStore({
	id: "searchStore",
	state: () => {
		const commonStore = useCommonStore()
		const localKey = "history-" + commonStore.region_id
		return {
			localKey,
			historyList: getLocalStorage(localKey) || [],
		}
	},
	actions: {
		updateHistoryList(payload) {
			this.historyList = payload
			setLocalStorage(this.localKey, payload)
		},
		clearHistoryList() {
			this.historyList = []
			removeLocalStorage(this.localKey)
		},
	},
})

export default useSearchStore
